package com._21cn.cms.yellow.web.controller;

import com._21cn.cms.yellow.entity.YellowPagePicture;
import com._21cn.cms.yellow.service.YellowPagePictureService;
import com._21cn.framework.base.controller.BaseController;
import com._21cn.framework.flip.PageList;
import com._21cn.framework.http.HttpRequestInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping({"/admin/yellow/yellowPagePicture"})
public class YellowPagePictureController extends BaseController
{
    private static String listView = "/admin/yellow/yellowPagePicture/list";
    private static String showView = "/admin/yellow/yellowPagePicture/show";
    private static String jsonView = "/admin/yellow/yellowPagePicture/json";
    private static String formView = "/admin/yellow/yellowPagePicture/form";
    private static String redirectListView = "redirect:/admin/yellow/yellowPagePicture/list.do";

    private Map<String, String> propertyTitleMap = null;

    @Autowired
    private YellowPagePictureService yellowPagePictureService;

    @Autowired(required=false)
    @Qualifier("yellowPagePictureValidator")
    private Validator yellowPagePictureValidator;

    @RequestMapping({"/list"})
    public String listYellowPagePicture(HttpServletRequest request, HttpServletResponse response, ModelMap model) { HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter("view", listView);
        setRequestModelMap(request, model, true);
        return view; }

    @RequestMapping({"/search"})
    public String searchYellowPagePicture(HttpServletRequest request, HttpServletResponse response, ModelMap model)
    {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter("view", jsonView);
        setRequestModelMap(request, model, true);
        PageList result = getPageList(model);
        model.addAttribute("objListJson", JSON.toJSONString(result, new SerializerFeature[] { SerializerFeature.WriteDateUseDateFormat }));
        return view;
    }

    @RequestMapping({"/show"})
    public String showYellowPagePicture(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter("view", showView);
        Integer id = Integer.valueOf(reqInfo.getIntParameter("id", -1));
        if (id.intValue() > 0) {
            YellowPagePicture yellowPagePicture = (YellowPagePicture)this.yellowPagePictureService.getById(id);
            if (yellowPagePicture == null) {
                model.put("error.param.wrong", "无效参数");
            }

            model.addAttribute("yellowPagePicture", yellowPagePicture);
        }
        setRequestModelMap(request, model);
        return view;
    }

    @RequestMapping({"/edit"})
    public String editYellowPagePicture(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter("view", formView);
        Integer id = Integer.valueOf(reqInfo.getIntParameter("id", -1));
        YellowPagePicture yellowPagePicture = new YellowPagePicture();
        if (id.intValue() > 0) {
            yellowPagePicture = (YellowPagePicture)this.yellowPagePictureService.getById(id);
            if (yellowPagePicture == null) {
                model.put("error.param.wrong", "无效参数");
            }
        }
        model.addAttribute("yellowPagePicture", yellowPagePicture);

        setRequestModelMap(request, model);
        return view;
    }

    @RequestMapping({"/save"})
    public String saveYellowPagePicture(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = Integer.valueOf(reqInfo.getIntParameter("id", -1));
        if (id.intValue() > 0) {
            YellowPagePicture yellowPagePicture = (YellowPagePicture)this.yellowPagePictureService.getById(id);
            if (bind(request, yellowPagePicture, model, new Validator[] { this.yellowPagePictureValidator }).hasErrors()) {
                return formView;
            }
            this.yellowPagePictureService.update(yellowPagePicture);
        } else {
            YellowPagePicture yellowPagePicture = new YellowPagePicture();
            if (bind(request, yellowPagePicture, model, new Validator[] { this.yellowPagePictureValidator }).hasErrors()) {
                return formView;
            }
            this.yellowPagePictureService.insert(yellowPagePicture);
        }
        model.clear();
        return redirectListView + "?" + reqInfo.getParameter("queryParam", "");
    }

    @RequestMapping({"/updateStatus"})
    public String updateYellowPagePictureStatus(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer status = Integer.valueOf(reqInfo.getIntParameter("status", -1));
        if (status.intValue() < 0) {
            model.clear();
            return redirectListView + "?" + reqInfo.getParameter("queryParam", "");
        }
        setRequestModelMap(request, model);
        String objectIds = getGetObjectIds(reqInfo, model);
        if (objectIds.length() > 0) {
            this.yellowPagePictureService.update(model);
        }
        model.clear();
        return redirectListView + "?" + reqInfo.getParameter("queryParam", "");
    }

    @RequestMapping({"/delete"})
    public String deleteYellowPagePicture(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        String objectIds = getGetObjectIds(reqInfo, model);
        if (objectIds.length() > 0) {
            this.yellowPagePictureService.delete(model);
        }
        model.clear();
        return redirectListView + "?" + reqInfo.getParameter("queryParam", "");
    }

    private PageList<YellowPagePicture> getPageList(ModelMap model) {
        if (model.containsKey("likeName"))
            model.put("likeName", model.get("likeName").toString().toLowerCase());
        PageList yellowPagePictureList = this.yellowPagePictureService.getList(model, Integer.parseInt(model.get("pageNo").toString()), Integer.parseInt(model.get("pageSize").toString()));
        model.addAttribute("yellowPagePictureList", yellowPagePictureList);
        model.addAttribute("objList", yellowPagePictureList);
        model.addAttribute("pageTurn", yellowPagePictureList.getPageTurn());
        model.addAttribute("titleMap", getPropertyTitleMap());
        return yellowPagePictureList;
    }

    private Map<String, String> getPropertyTitleMap() {
        if (this.propertyTitleMap == null) {
            this.propertyTitleMap = new HashMap();
            this.propertyTitleMap.put("id", "id");
            this.propertyTitleMap.put("yellowPageId", "yellowPageId");
            this.propertyTitleMap.put("picHashcode", "picHashcode");
            this.propertyTitleMap.put("picUrl", "picUrl");
            this.propertyTitleMap.put("orderNum", "orderNum");
            this.propertyTitleMap.put("status", "status");
            this.propertyTitleMap.put("createTime", "createTime");
        }
        return this.propertyTitleMap;
    }
}